/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import handler.BrugerHandler;
import handler.HandlerFactory;
import handler.MenuHandler;
import handler.ProduktHandler;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import model.Bestilling;
import model.Bruger;
import model.Produkt;
import utility.NotLoggedInException;

/**
 *
 * @author Jeppe
 */
public class OrderController {

    private Bruger aktivBruger;
    private static OrderController instance;

    public static final int DEADLINETIME = 21;
    public static final int DEADLINEMINUT = 00;
    //<editor-fold defaultstate="collapsed" desc="Handler Variables">
    private BrugerHandler brugerHandler;
    private MenuHandler menuHandler;
    private ProduktHandler produktHandler;
//</editor-fold>

    HandlerFactory handlerFactory;

    private OrderController() {
        this.handlerFactory = HandlerFactory.getInstance();
        this.brugerHandler = handlerFactory.getBrugerHandler();
        this.menuHandler = handlerFactory.getMenuHandler();
        this.produktHandler = handlerFactory.getProduktHandler();
    }

    public static OrderController getInstance() {
        if (instance == null) {
            instance = new OrderController();
        }
        return instance;
    }

    //<editor-fold defaultstate="collapsed" desc="BrugerHandling">
    public boolean login(String tlfEmail, String password) throws SQLException {
        boolean login = false;
        if (tlfEmail.contains("@")) {
            System.out.println("Email");
            if (brugerHandler.isPasswordCorrectEmail(tlfEmail, password)) {
                login = true;
                aktivBruger = brugerHandler.getBrugerByEmail(tlfEmail);
                Observer.getInstance().notifyListeners(aktivBruger, "Bruger");
            }
        } else if (tlfEmail.matches("\\d+")) {
            if (brugerHandler.isPasswordCorrectTlf(tlfEmail, password)) {
                login = true;
                aktivBruger = brugerHandler.getBrugerByTlf(tlfEmail);
                Observer.getInstance().notifyListeners(aktivBruger, "Bruger");
            }
        }
        return login;
    }

    public void logOut() {
        aktivBruger = null;
        Observer.getInstance().notifyListeners(this, "Bruger");
    }

    public Bruger getAktivBruger() {
        return aktivBruger;
    }
//</editor-fold>

    public void lægProduktIKurv(Calendar date, Produkt produkt) throws NotLoggedInException {
        SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
        if (aktivBruger != null) {
            for (int i = 0; i < aktivBruger.getKurv().size(); i++) {
                if (aktivBruger.getKurv().get(i).getCalendar().compareTo(date)==0) {
                    System.out.println("De to datoer er ens!!!!");
                }
            }
//            System.out.println(produkt.getTitel() + " er tilføjet til kurven.");
        } else {
            throw new NotLoggedInException();
        }
    }

}
